package cn.com.cfae.iras.doc.analyze.components.integrity;

import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractBizModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import cn.com.cfae.iras.doc.analyze.parse.model.FXTKModel;
import cn.com.cfae.iras.doc.analyze.parse.model.SYModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * Created by 佟媛媛 on 18/10/23.
 */

@Component("I1015")
@Scope("prototype")
public class I1015 extends BaseComponent {

    private static Logger log = LoggerFactory.getLogger(I1015.class);


    /**
     * @auth 佟媛媛
     * @date 18/10/29 下午7:13
     * @desc
     * 是否包含以下内容的释义，
     * “发行人”、
     * “债务融资工具”、
     * “募集说明书”、
     * “主承销商”、
     * “承销团”、
     * “承销协议”、
     * “簿记建档”、
     * “簿记管理人”、、
     * “交易商协会”、
     * “上清所（或上海清算所）”、
     * “北金所”、
     * “三年及一期”。
     * 若不同时含有前述字样，提示“该文档重要释义不完备”
     * "1、检查关键词是否存在，不存在提示缺失；2、拆分为12项释义；"
     */
    @Override
    public List<ComponentResult> handlerM() {
        return common("M-1");
    }

    private List<ComponentResult> common(String catalogId){
        setCatalogId(catalogId);
        List<ComponentResult> list = new ArrayList<>();
        String[] values = new String[]{"FXR","ZWRZGJ","MJSMS","ZCXS","CXT","CXXY","BJJD","BJGLR","JYSXH","SQS","BJS","SNJYQ"};
        int pageNum = getChapterModel(getCatalogId()).getStartPage();
        for(String v : values){
            KVText value = getKVTextKey(getCatalogId(),"SY",v);
            list.add(getComponentResult(value,pageNum,null,"重要释义-"+getKeyword(v),"重要释义","该文档重要释义不完备"));
        }
        return list;
    }

    @Override
    public List<ComponentResult> handlerBM() {
        return common("BM-1");
    }

    @Override
    public List<ComponentResult> handlerDM() {
        return common("DM-1");
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        return common("BDM-1");
    }

    private ComponentResult getComponentResult(KVText value,int pageNum,String text,String keyword,String ruleName,String promptMsg){
        String exist = "02";
        boolean annotationFlag = true;
        List<ComponentResult.TextPosition> textPositionList = null;
        try{
            if(value!=null){
                annotationFlag = false;
                pageNum = value.getPageNumber();
                exist = "01";
                textPositionList = getContentAndPositionList(value);
            }
            return getComponentResult(annotationFlag,exist,keyword,pageNum,promptMsg,ruleName,textPositionList);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return getComponentResult(annotationFlag,exist,keyword,pageNum,promptMsg,ruleName,textPositionList);
        }
    }

    private String getKeyword(String key){
        List<ExtractItem> list = SYModel.createExtractItemList();
        if(CollectionUtils.isEmpty(list)){
            return getRuleTitle();
        }
        for(ExtractItem item : list){
            if(key.equals(item.getItemName())){
                return item.getSynonyms().length==0?getRuleTitle():item.getSynonyms()[0];
            }
        }
        return getRuleTitle();
    }

    private ComponentResult getComponentResult(boolean annotationFlag,String exist,String keyword,int pageNum,String promptMsg,String ruleName,List<ComponentResult.TextPosition> textPositionList){
        ComponentResult result = new ComponentResult();
        result.setAnnotationFlag(annotationFlag);
        result.setCatalogId(getCatalogId());
        result.setChapterId(getChapterId());
        result.setExist(exist);
        result.setKeyword(keyword);
        result.setPageNum(pageNum);
        result.setPromptMessage("01".equals(exist)?"正确":promptMsg);
        result.setRegulationId(getRegulationId());
        result.setRuleName(ruleName);
        result.setTextPosition(textPositionList);
        result.setUid(UUID.randomUUID().toString());
        return result;
    }


}
